package com.food.eat.impl;

import com.food.eat.mapper.ItemsCommentsCustomMapper;
import com.food.eat.mapper.SearchItemsMapper;
import com.food.eat.pojo.bo.ShopCatBo;
import com.food.eat.pojo.vo.ItemCommentVo;
import com.food.eat.pojo.vo.SearchItemsVo;
import com.food.eat.service.ItemCommentService;
import com.food.eat.service.ItemsService;
import com.food.eat.util.DesensitizationUtil;
import com.food.eat.util.PagedGridResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javafx.print.Collation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author:RenPu
 * @Date: 2020/7/21 17:39
 * @Version: 1.0
 * @description: 用户评论信息的展示以及分页的实现
 */

@Service
public class ItemCommentServiceImpl implements ItemCommentService {


    @Resource
    private ItemsCommentsCustomMapper itemsCommentsCustomMapper;

    @Resource
    private SearchItemsMapper searchItemsMapper;


    /**
     * 根据商品的唯一id值，以及评价的等级同时筛选评论详细信息实现分页
     *
     * @param level
     * @param itemId
     * @param page
     * @param pagesize
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedGridResult queryByItemsIdCommentInfo(Integer level, String itemId, Integer page, Integer pagesize) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("level", level);
        map.put("itemId", itemId);

        //mybatis-PageHelp 使用插件的分页实现

        PageHelper.startPage(page, pagesize);


        List<ItemCommentVo> itemCommentVos = itemsCommentsCustomMapper.queryByItemsIdCommentInfo(map);

        for (ItemCommentVo itemCommentVo : itemCommentVos) {

            //用于用户敏感信息的脱敏，例如：将任我行，脱敏为任*行
            itemCommentVo.setNickname(DesensitizationUtil.commonDisplay(itemCommentVo.getNickname()));

        }

        PagedGridResult pagedGridResult = setterPage(itemCommentVos, page);

        return pagedGridResult;
    }


    /**
     * 首页商品搜索详情展示功能以及分页的实现
     *
     * @param sort
     * @param keyWords
     * @param page
     * @param pagesize
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedGridResult searchItemsMessageInfo(String keyWords, String sort, Integer page, Integer pagesize) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("keyWords", keyWords);
        map.put("sort", sort);


        //mybatis-PageHelp 使用插件的分页实现

        PageHelper.startPage(page, pagesize);
        List<SearchItemsVo> searchItemsVos = searchItemsMapper.searchItemsMessageInfo(map);

        PagedGridResult pagedGridResult = setterPage(searchItemsVos, page);


        return pagedGridResult;
    }


    /**
     * 在首页点击三级类别，根据三级类别的id值进行查询并分页，所获取到的数据信息
     *
     * @param catId
     * @param sort
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PagedGridResult searchItemsByCatIdType(Integer catId, String sort, Integer page, Integer pagesize) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("catId", catId);
        map.put("sort", sort);

        //mybatis-PageHelp 使用插件的分页实现

        PageHelper.startPage(page, pagesize);
        List<SearchItemsVo> searchItemsVos = searchItemsMapper.searchItemsByCatIdType(map);

        PagedGridResult pagedGridResult = setterPage(searchItemsVos, page);
        return pagedGridResult;
    }


    /**
     * 根据规格ids查询最新的购物车中商品数据（用于刷新渲染购物车的数据）
     *
     * @param specIds
     * @return
     */
    @Override
    public List<ShopCatBo> queryItemsBySpecId(String specIds) {

        //将前端传来的字符串进行分割，例如：“1001,2001,300,4001”
        String[] split = specIds.split(",");
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, split);
        List<ShopCatBo> shopCatBos = itemsCommentsCustomMapper.queryItemsBySpecId(list);
        return shopCatBos;
    }


    /**
     * 将mybatis的分页方法提取出来，作为公共用
     *
     * @param list
     * @param page
     * @return
     */
    public PagedGridResult setterPage(List<?> list, Integer page) {
        PageInfo<?> pageInfo = new PageInfo<>(list);
        PagedGridResult gridResult = new PagedGridResult();
        gridResult.setRows(list);
        gridResult.setPage(page);
        gridResult.setTotal(pageInfo.getPages());
        gridResult.setRecords(pageInfo.getTotal());

        return gridResult;

    }
}
